import { reqCategoryList, reqFloorList, reqGetBannerList } from '@/api'
// home 模块小仓库
const state = {
  // state 值不能瞎写，服务器返回的是对象或数组  根据接口的初始值设置的
  categoryList: [],
  // banner 轮播图模拟数据
  bannerList: [],
  // floor 家用电器数据
  floorList: []
}
// 唯一修改 state 的地方
const mutations = {
  CATEGORYLIST (state, categoryList) {
    // 修改的数据等于服务器返回的数据
    state.categoryList = categoryList
  },
  // banner 轮播图
  GETBANNERLIST (state, bannerList) {
    state.bannerList = bannerList
  },
  // floor 家用电器
  GETFLOORLIST (state, floorList) {
    state.floorList = floorList
  }
}
const actions = {
  // 返回的是一个 promise 对象
  async categoryList ({ commit }) {
    const result = await reqCategoryList()
    if (result.code === 200) {
      // 提交的数据  result.data
      commit('CATEGORYLIST', result.data)
    }
  },
  // 获取轮播图模拟的mock数据
  async getBannerList ({ commit }) {
    const result = await reqGetBannerList()
    if (result.code === 200) {
      commit('GETBANNERLIST', result.data)
    }
  },
  // 获取家用电器的mock数据
  async getFloorList ({ commit }) {
    const result = await reqFloorList()
    if (result.code === 200) {
      commit('GETFLOORLIST', result.data)
      // console.log(result.data)
    }
  }
}
// 计算属性
const getters = {}

// 对外暴露
export default {
  state,
  mutations,
  actions,
  getters
}
